<?php
// +----------------------------------------------------------------------
// | 商品品牌
// +----------------------------------------------------------------------
// | Author: myh
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\common\lib\oss\Oss;
use app\admin\service\GoodsBrandService;

class GoodsBrand extends AdminController
{
    //分页
    public function page(){
        return GoodsBrandService::page($this->request->get());
    }

    //新增
    public function add(){
        $data = $this->request->post();
        //验证规则
        $validate = [
            'cate_id'  => 'require',
            'brand_name'  => 'require',
            'brand_image'  => 'require'
        ];
        //提示信息
        $message = [
            'cate_id.require' => '请选择分类!',
            'brand_name.require' => '品牌名称不能为空!',
            'brand_image.require' => '请上传品牌logo!',
        ];

        $this->validate($data, $validate, $message);
        return GoodsBrandService::save($data);
    }

    //更新
    public function edit(){
        $data = $this->request->post();
        //验证规则
        $validate = [
            'id' => 'require',
            'cate_id'  => 'require',
            'brand_name'  => 'require',
            'brand_image'  => 'require'
        ];
        //提示信息
        $message = [
            'id.require' => '请选择要更新的数据!',
            'cate_id.require' => '请选择分类!',
            'brand_name.require' => '品牌名称不能为空!',
            'brand_image.require' => '请上传品牌logo!',
        ];

        $this->validate($data, $validate, $message);
        return GoodsBrandService::update($data);
    }

    public function changeStatus(){
        $data = $this->request->post();
        //验证规则
        $validate = [
            'id' => 'require',
            'is_show'  => 'require'
        ];
        //提示信息
        $message = [
            'id.require' => '缺少ID参数!',
            'is_show.require' => '缺少is_show参数!'
        ];

        $this->validate($data, $validate, $message);
        return GoodsBrandService::update($data);
    }

    //删除
    public function delete(){
        $ids = $this->request->get("ids");
        if(empty($ids)){
            return failure(config('error.er5')['code'],"请选择要删除的数据");
        }

        return GoodsBrandService::destroy($ids);
    }

    //上传logo
    public function uploadLogo(){
        $file = $this->request->file('file');
        $pathName = $file->getPathname();
        $originalName = $file->getOriginalExtension();

        $object = "goods/logo/".$file->hash('md5').".".$originalName;
        return success(Oss::uploadFile($object,$pathName));
    }

}
